package es.javamyadmin.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet que se ejecuta cuando el usuario cierra sesion
 * @author Emilio Javier Castillo Gonzalez
 * @author Hector Delgado Duran
 *
 */
public class CerrarSesion extends HttpServlet {

	

	/**
	 * 
	 */
	private static final long serialVersionUID = 196253250330885880L;

	/**
	 * Metodo que llama a doPost
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * Metodo que cierra la sesion, eliminando la cookie si la hubiera.
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Cookie cookies[] = request.getCookies();
		for(int i = 0; i< cookies.length; i++){
			cookies[i].setValue("");
            cookies[i].setPath("/");
            cookies[i].setMaxAge(0);
            response.addCookie(cookies[i]);
		}
		HttpSession sesion = request.getSession(true);
		sesion.invalidate();
		response.sendRedirect("index.jsp");
	}

}
